package xyz.luhui1hao.sort;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @ClassName SortMain
 * @Description 排序
 * 比较 Arrays 和 ArrayLists。
 * Arrays：一个包含许多和操纵数组有关方法的类，比如排序和查找，它继承自Object类。
 * ArraysList：是一个容器，它可以实现数组的大小可变，方便地增加和删除元素。它实现了List接口的类。
 * @Author luhui1hao
 * @Date 2018/9/25 4:07 PM
 * @Version 1.0
 */
public class SortMain {
    public static void main(String args[]) {
        Person[] persons = getData();

        System.out.println("未排序的原始数据");
        System.out.println(Arrays.toString(persons));

        //Comparabale测试
        comparableFun(persons);
        //Comparator测试
        comparatorFun(persons);
    }

    private static void comparableFun(Person[] persons) {
        Arrays.sort(persons);

        System.out.println("通过Comparable接口排序的数据");
        System.out.println(Arrays.toString(persons));
    }

    private static void comparatorFun(Person[] persons) {
        /**
         * 开始排序
         */
        AgeComparator ageComparator = new AgeComparator();
        //数组转集合
        List personsList = Arrays.asList(persons);
        //升序排序
        personsList.sort(ageComparator);
        //反转
        Collections.reverse(personsList);
        //集合转数组
        Person[] sortPersons = (Person[]) personsList.toArray();

        System.out.println("通过Comparator排序的数据");
        System.out.println(Arrays.toString(sortPersons));
    }

    private static Person[] getData() {
        Person person1 = new Person();
        person1.setName("老大");
        person1.setAge(30);
        person1.setSex(SexEnum.MALE);
        person1.setPower(60);

        Person person2 = new Person();
        person2.setName("老二");
        person2.setAge(28);
        person2.setSex(SexEnum.FEMALE);
        person2.setPower(40);

        Person person3 = new Person();
        person3.setName("老三");
        person3.setAge(20);
        person3.setSex(SexEnum.MALE);
        person3.setPower(75);

        Person person4 = new Person();
        person4.setName("老四");
        person4.setAge(18);
        person4.setSex(SexEnum.MALE);
        person4.setPower(70);

        Person person5 = new Person();
        person5.setName("老五");
        person5.setAge(10);
        person5.setSex(SexEnum.MALE);
        person5.setPower(98);

        return new Person[]{person4, person5, person2, person1, person3};
    }
}
